package nc.impl.bd.defdoc;

import java.util.Collection;
import java.util.Iterator;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.itf.bd.userdefrule.IUserdefruleService;
import nc.vo.bd.defdoc.DefdoclistVO;
import nc.vo.bd.userdefrule.UserdefruleVO;
import nc.vo.bd.userdefrule.UserdefrulerefVO;
import nc.vo.ml.LanguageVO;
import nc.vo.ml.MultiLangContext;
import nc.vo.pub.BusinessException;

public class DefdocUserdefItemRegister
{
  private static final int DEFDOC_USERDEF_COUNT = 200;
  private static final String UAPBD_USERDEFRULE_ID = "1001Z0100000000008O3";
  public IUserdefruleService service = null;

  public IUserdefruleService getService() {
    if (this.service == null) {
      this.service = ((IUserdefruleService)NCLocator.getInstance().lookup(IUserdefruleService.class));
    }
    return this.service;
  }

  public void registerUserdefitemRule(DefdoclistVO defdoclist) throws BusinessException {
    UserdefruleVO ruleVO = new UserdefruleVO();
    fillUserdefruleVO(defdoclist, ruleVO);
    ruleVO = getService().inertUserdefruleVO(ruleVO);
    UserdefrulerefVO refVO = generateUserdefrulerefVO(defdoclist, ruleVO);
    getService().insertUserdefrulerefVO(refVO);
  }

  private void fillUserdefruleVO(DefdoclistVO defdoclist, UserdefruleVO ruleVO) throws BusinessException {
    ruleVO.setPk_org("GLOBLE00000000000000");
    ruleVO.setPk_group(InvocationInfoProxy.getInstance().getGroupId());
    ruleVO.setCode(new StringBuilder().append("Defdoc-").append(defdoclist.getCode()).toString());
    fillUserdefName(defdoclist, ruleVO);
    ruleVO.setItemcount(Integer.valueOf(200));
    ruleVO.setParent_id("1001Z0100000000008O3");
    ruleVO.setRuletype(Integer.valueOf(0));
  }

  private void fillUserdefName(DefdoclistVO defdoclist, UserdefruleVO ruleVO) throws BusinessException {
    LanguageVO[] langs = MultiLangContext.getInstance().getEnableLangVOs();
    for (LanguageVO language : langs) {
      int index = language.getLangseq().intValue();
      String defdocListName = (String)defdoclist.getAttributeValue(getNameByIndex("name", index));
      ruleVO.setAttributeValue(getNameByIndex("name", index), defdocListName);
    }

    ruleVO.setName(new StringBuilder().append("defdoc_").append(defdoclist.getName()).toString());
  }

  private String getNameByIndex(String name, int index) {
    return new StringBuilder().append(name).append(index == 1 ? "" : Integer.valueOf(index)).toString();
  }

  private UserdefrulerefVO generateUserdefrulerefVO(DefdoclistVO defdoclist, UserdefruleVO ruleVO) {
    UserdefrulerefVO ref = new UserdefrulerefVO();
    ref.setPk_userdefrule(ruleVO.getPk_userdefrule());
    ref.setRefclass(defdoclist.getPk_defdoclist());
    ref.setPrefix("def");
    ref.setCheckclass(null);
    return ref;
  }

  public void updateUserdefitemRule(DefdoclistVO defdoclist, DefdoclistVO oldDefdoclist) throws BusinessException {
    UserdefruleVO ruleVO = queryUserdefruleVO(oldDefdoclist);

    if (ruleVO == null) {
      registerUserdefitemRule(defdoclist);
    } else {
      fillUserdefruleVO(defdoclist, ruleVO);
      getService().updateUserdefruleVO(ruleVO);
    }
  }

  public void deleteUserdefitemRule(DefdoclistVO defdoclist) throws BusinessException {
    getService().deleteUserdefruleVO(queryUserdefruleVO(defdoclist));
  }

  private UserdefruleVO queryUserdefruleVO(DefdoclistVO defdoclist) throws BusinessException
  {
    Collection c = new BaseDAO().retrieveByClause(UserdefruleVO.class, new StringBuilder().append("pk_userdefrule in (select pk_userdefrule from ")
      .append(UserdefrulerefVO.getDefaultTableName()).append(" where ").append("refclass").append(" = '")
      .append(defdoclist
      .getPk_defdoclist()).append("')").toString());
    return c.isEmpty() ? null : (UserdefruleVO)c.iterator().next();
  }
}